/* eslint-disable import/no-duplicates */
/* eslint-disable no-undef */

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'

// 解决重复点击菜单栏路由的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    redirect: '/login' // 重定向
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/Roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// eslint-disable-next-line no-undef
// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果用户访问的是登录页  直接放行
  if (to.path === '/login') return next()
  // 从session中取到保存的token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token  强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
export default router
